/**
 * Created by smp on 2017/6/8.
 */
var GLOBAL = {
    ID: $.getQueryParam("id"),
    ACTION_URL: ROOT_PATH + '/workLicense/electricityLicense/add.shtml'
};
var ajaxSuccessHandler = function (result) {
    result = $.parseJSON(result);
    if (result.success) {
        $.messager.progress('close');	// 如果提交成功则隐藏进度条
        layer.msg(result.message, {time: 1000, icon: 1}, function () {
            WindowManager.closeLayer({returnValue: true});
        });
    } else {
        $.messager.progress('close');	// 如果提交成功则隐藏进度条
        layer.msg(result.message, {icon: 2});
    }
};
/*var addCommonFormParams = function (param) {
    param.operatorName = $('#operatorName').combobox('getText');
    param.fieldGuardianName = $('#operatorName').combobox('getText');
};*/
var formSubmitAction = function (url, params) {
    $.messager.progress();	// 显示进度条

    $('#form1').form('submit', {
        url: url,
        onSubmit: function (param) {
           // addCommonFormParams(param);
            if ($.isNotEmpty(params)) {
                $.extend(param, params);
            }
            var isValid = $(this).form('validate');
            if (!isValid) {
                $.messager.progress('close');	// 如果表单是无效的则隐藏进度条
            }
            return isValid;	// 返回false终止表单提交
        },
        success: function (result) {
            ajaxSuccessHandler(result);
        }
    });
};
var getSelectedContractorAudit = function (data) {
    data = data[0];
    $('#contractorName').textbox('setValue', data.contractorName);
    $('#contractorAuditId').val(data.id);
};
var getSelectedApplyUser = function (data) {
    data = data[0];

    $('#applyPersonName').textbox('setValue', data.realName);
    $('#applyPerson').val(data.id);
};
var getSelectedWorkUser = function (data) {
    var realName=data[0].realName;
    var id=data[0].id;
    for(var i=1;i<data.length;i++){
        realName=realName+","+data[i].realName;
        id=id+","+data[i].id;
    }
    $('#operatorName').textbox('setValue', realName);
    $('#operator').val(id);
};
var getSelectedGuardUser = function (data) {
    var realName=data[0].realName;
    var id=data[0].id;
    for(var i=1;i<data.length;i++){
        realName=realName+","+data[i].realName;
        id=id+","+data[i].id;
    }
    $('#fieldGuardianName').textbox('setValue', realName);
    $('#fieldGuardian').val(id);
};
var openSelectUser = function (title, callback) {
    WindowManager.openLayer({
        type: 2,
        title: title,
        area: ['900px', '500px'],
        content: ROOT_PATH + '/permission/user/simpleList.shtml?isMulti=1',
        callBack: callback
    });
};
$(function () {


    $('#hazardIdentification').combobox({
        url: ROOT_PATH + '/permission/dictionary/queryDictionaryByDictionaryTypeCode.shtml?code=' + CONSTANT.DictionaryTypeDefConstants.DANGER_TYPE,
        valueField: 'id',
        editable: false,
        multiple: true,
        required: true
    });

    $('#applyPersonName').textbox({
        required: true,
        icons: [{
            iconCls: 'icon-search',
            handler: function () {
                openSelectUser('选择申请人', getSelectedApplyUser)
            }
        }],
        editable: false
    });
    $('#operatorName').textbox({
        required: true,
        icons: [{
            iconCls: 'icon-search',
            handler: function () {
                openSelectUser('选择作业人', getSelectedWorkUser)
            }
        }],
        editable: false
    });
    $('#fieldGuardianName').textbox({
        required: true,
        icons: [{
            iconCls: 'icon-search',
            handler: function () {
                openSelectUser('选择现场监护人', getSelectedGuardUser)
            }
        }],
        editable: false
    });
    $('#applyUnit').combotree({
        url: ROOT_PATH + '/permission/department/getDepartmentTree.shtml?parentId=0',
        editable: false
    });

    $('#btn_save').on('click', function () {
        formSubmitAction(GLOBAL.ACTION_URL);
    });
});